<?php

namespace app\exam\controller;

use app\exam\model\GoodsBrand;
use app\exam\model\GoodsType;
use think\Controller;
use think\Loader;
use think\Request;

class Exam extends Controller
{
    //展示添加列表
    public function create()
    {
        $brandInfo = GoodsBrand::selectBrand();
        $typeInfo = GoodsType::selectType();
//        print_r($brandInfo);die();
        $this->assign('brandInfo', $brandInfo);
        $this->assign('typeInfo', $typeInfo);
        return view();
    }

//    执行添加操作
    public function add()
    {
//        接收数据
        $data = $this->request->param();

//        接收文件
        $file = request()->file('good_img');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $info = $file->validate(['size' => 1024 * 1024 * 2, 'ext' => 'jpeg,jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info) {
                $fileImg = DS . 'uploads' . DS . $info->getSaveName();
                $data['good_img'] = $fileImg;
            } else {
                $this->error($file->getError(), 'exam/exam/create');
            }
        }
//        print_r($data);die();
        //        验证规则
        $validate = Loader::validate('Goods');
        if (!$validate->scene('goods')->check($data)) {
            $this->error($validate->getError());
        }
//        添加入库
        $res = \app\exam\model\Exam::add($data);
        if ($res) {
            $this->redirect('/exam/exam/index');
        }
    }

    //商品列表展示
    public function index()
    {
        $data = \app\exam\model\Exam::index();
        $this->assign('data', $data);
        return view();
    }
     // 修改状态
    public function update(Request $request){
            $data = $request->param();

        $res = \app\exam\model\Exam::upd($data);

        return json(['status'=>200,'info'=>'ok','data'=>$res]);
    }
}
